package com.erp.controller;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.Utils.DateFormat;
import com.erp.Utils.ResponseModel;
import com.erp.entity.Project;
import com.erp.enums.statusCode;
import com.erp.mapper.ProjectMapper;
import com.erp.service.ProjectService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.*;

/**
 * <p>
 * 项目管理 前端控制器
 * </p>
 *
 * @author  CJ 
 * @since 2021-08-16
 */
@Controller
@RequestMapping("/project")
public class ProjectController {
    @Resource
    ProjectService projectService;

    @RequestMapping(value = "listView",method = RequestMethod.GET)
    public ModelAndView listView(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/project/project_list");
        return mav;
    }
    @RequestMapping(value = "/getListData",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> getListData(@RequestParam(value = "page") int page,
                                          @RequestParam(value = "limit") int limit,
                                          @RequestParam(value = "projectName",required = false) String projectName,
                                          @RequestParam(value = "start",required = false) String start,
                                          @RequestParam(value = "end",required = false) String end){
        QueryWrapper<Project> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(projectName)){
            wrapper.like("project_name",projectName);
        }
        if (StrUtil.isNotEmpty(start) && StrUtil.isNotEmpty(end)){
            wrapper.ge("create_time",start).le("create_time",end);
        }
        IPage<Project> userPage = new Page<>(page, limit);//参数一是当前页，参数二是每页个数
        if ((StrUtil.isNotEmpty(start) && StrUtil.isNotEmpty(end)) || StrUtil.isNotEmpty(projectName)){
            userPage = projectService.page(userPage, wrapper);
        }else{
            userPage = projectService.page(userPage, null);
        }
        List<Project> projectList = userPage.getRecords();//获取数据，这里面还有当前页码数还有总数据参数
//        List<Project> projectList = projectService.list(new QueryWrapper<Project>());
//        int count = projectList.size();
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",userPage.getTotal());
        map.put("data",projectList);
        return map;
    }

    @RequestMapping(value = "editView",method = RequestMethod.GET)
    public ModelAndView editView(@RequestParam int id){
        Project project = projectService.getById(id);
        ModelAndView mav = new ModelAndView();
        mav.addObject("project",project);
        mav.setViewName("project/editView");
        return mav;
    }

    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public ResponseModel edit(@RequestBody JSONObject data){
        String projectName = (String) data.get("projectName");
        String projectInfo = (String) data.get("projectInfo");
        Project project = new Project();
        project.setId(Convert.toInt(data.get("projectId")));
        project.setProjectName(projectName);
        project.setProjectInfo(projectInfo);
        boolean status = projectService.updateById(project);
        if (status){
            return ResponseModel.success("修改成功");
        }else{
            return ResponseModel.fail("修改失败");
        }
    }

    @RequestMapping(value = "addView",method = RequestMethod.GET)
    public ModelAndView addView(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("project/addView");
        return mav;
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public ResponseModel add(@RequestBody JSONObject data){
        String projectName = (String) data.get("projectName");
        String projectInfo = (String) data.get("projectInfo");
        Project project = new Project();
        project.setProjectName(projectName);
        project.setProjectInfo(projectInfo);
        project.setCreateTime(DateFormat.getTime());
        project.setStatus(statusCode.Default.getValue());
        projectService.save(project);
        return ResponseModel.success();
    }
    //删除
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public ResponseModel del(@RequestBody JSONObject data){
        boolean status = projectService.removeById((int)data.get("projectId"));
        if (status){
            return ResponseModel.success("删除成功");
        }else{
            return ResponseModel.fail("删除失败");
        }
    }
    // 批量删除
    @RequestMapping(value = "/batch/delete",method = RequestMethod.POST)
    @ResponseBody
    public ResponseModel batchDel(@RequestBody  List<Integer> idList){
        boolean status = projectService.removeByIds(idList);
        if (status){
            return ResponseModel.success("删除成功");
        }else{
            return ResponseModel.fail("删除失败");
        }
    }
}
